

import UIKit

class YYTabBar: UITabBar {


    //第一步:定义一个闭包属性

    var closure: (()->())?

     // MARK: - 代码实现自定义视图

    //override 子类要去扩展父类的方法
    override init(frame: CGRect) {
        super.init(frame: frame)


        //添加 button
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }


    //添加子控件和设置子控件
    private func setupUI(){

        addSubview(compose2Button)
    }

    override func layoutSubviews() {
        //一定要调用父类的   super.layoutSubviews()
        super.layoutSubviews()

        //定义一个索引
        var index: CGFloat = 0
        //设置按钮的宽度
        let width = bounds.width / 5
        //通过遍历 tabbar 拿到子控件
        for subview in subviews{
         //   printLog(subview)

            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
        //        print("这个是要移动的位置")

            subview.frame = CGRectMake(index*width, 0, width, bounds.height)

                index += 1

                if index == 2{
                    index += 1
                }
            }
        }
        //修改 button 的位置
       compose2Button.frame = CGRectMake(width*2, 0, width, bounds.height)
    }

    func showPublish(){
        closure?()


    }

    lazy var composeButton = UIButton(type: UIButtonType.Custom)

    lazy var compose2Button: UIButton = {

        let button = UIButton(type:UIButtonType.Custom)

        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)

        //button大小
        button.sizeToFit()

        button.addTarget(self, action: "showPublish", forControlEvents: UIControlEvents.TouchUpInside)

        return button
    }()

}
